<?php
/**
 * Created by PhpStorm.
 * User: xiazc01
 * Date: 2018/6/13
 * Time: 14:23
 */
namespace common\repositories\myhtsq;

use common\entities\myhtsq\IncomeItemLogEntity;
use common\repositories\BaseRepository;

class IncomeItemLogRepository extends BaseRepository{


    static $BUSINESS_TYPE_ID_SET = [
        '51a24c9f-7338-11e7-aa68-2c44fd7fa0a8',
        '7ddf9628-ff75-4143-9d86-9c70c2782c30',
        '8fd17068-5eb4-4588-816d-8376bba2caf7',
        '96c73024-b239-4236-b8d9-469746ca801a',
        'a7e81732-3d27-45ea-b056-929fe9ed9f1d',
        'a8d08aa9-baad-4279-bbe1-1d05852b9e38',
        'f242b07c-df8a-4703-8005-eb12ab10416e'
    ];
    static $BUSINESS_TYPE_NAME_SET = [
        '云链平台收入项',
        '云链平台收入项',
        '云链租用服务收入项',
        '云链平台收入项',
        '云链线下服务收入项',
        '云链增值服务收入项',
        '云链增值服务收入项'
    ];

    const OPERATE_DELETE = 'delete';
    const OPERATE_ADD = 'add';
    const OPERATE_EDIT = 'edit';

    public function __construct(array $config = [])
    {
        parent::__construct($config);
    }

    public static function add($before,$now,$success,$operate){
        if(empty($before) && empty($now)){
            return true;
        }
        $map = array_flip(static::$BUSINESS_TYPE_ID_SET);
        $business_type = $now['revenue_type'];
        $key = $map[$business_type];

        if($key >= 0){
            $business_type_name = static::$BUSINESS_TYPE_NAME_SET[$key];
        }else{
            $business_type_name = '未知业务';
        }
        try {
            $date = date('Y-m-d H:i:s');
            $entity = new IncomeItemLogEntity();
            $entity->platform = 'yl';
            $entity->business_type = $business_type_name;
            $entity->contract_guid = $before?$before['contract_id']:$now['contract_id'];
            $entity->operated_on = $date;
            $entity->success = $success;
            $entity->operate = $operate;
            $entity->revenue_id = $before?$before['revenue_id']:$now['revenue_id'];
            $entity->model_diff = urldecode(json_encode(self::diff_model($before,$now,$operate,false)));
            $entity->explain_diff = urldecode(json_encode(self::diff_model($before,$now,$operate,true)));
            $result = $entity->save();
            if ($result === FALSE) {
                $error = $entity->getAllFirstErrorMessage();
                \Yii::info($error,'income_item_log');
            }
        } catch (\Exception $ex) {
            \Yii::info($ex->getMessage(),'income_item_log');
        }
    }

    private static function diff_model($before,$now,$operate,$explain = false){
        $revenue_attribute_label = array(
            'revenue_id' => '收入ID',
            'contract_id' => '合同ID',
            'revenue_type' => '收入类型',
            'amount' => '金额',
            'created_on' => '创建时间',
            'created_by' => '创建人',
            'modified_on' => '修改时间',
            'modified_by' => '修改人',
            'income_code' => '收入项编码',
            'total_confirm_amount' => '累计已确认金额',
            'wait_confirm_amount' => '待确认收入',
            'carryover_begin_time' => '收入结转开始时间',
            'right_begin_time' => '权益开始时间',
            'right_end_time' => '权益截止时间',
            'no_confirm_bad_amount' => '未确认收入发生坏账',
            'have_confirm_bad_amount' => '已确认收入发生坏账',
            'kingdee_bad_debt_amount' => '金蝶核销坏账金额',
            'close_status' => '1:收入未关闭;2:收入通过手动关闭;3:收入自动关闭;4:已结转',
            'income_source' => '收入来源：本期新增0，上期结转1',
            'remark' => '备注',
        );

        $diff = array();
        if($operate == self::OPERATE_DELETE){
            $diff_arr = $before;
            if(!$explain){
                return $diff_arr;
            }
            foreach ($diff_arr as $key => $value){
                $label = $revenue_attribute_label[$key];
                $record = array('before'=>$diff_arr[$key]);
                $label = urlencode($label);
                $diff[$label] = $record;
            }

        }else if($operate == self::OPERATE_EDIT) {
            $diff_arr = array_diff_assoc($before,$now);
            if(!$explain){
                return $diff_arr;
            }
            foreach ($diff_arr as $key => $value){
                $label = $revenue_attribute_label[$key] ;
                $record = array('before'=>$before[$key],'now'=>$now[$key]);
                $label = urlencode($label);
                $diff[$label] = $record;
            }
        }else if($operate == self::OPERATE_ADD){
            $diff_arr = $now;
            if(!$explain){
                return $diff_arr;
            }
            foreach ($diff_arr as $key => $value){
                $label = $revenue_attribute_label[$key] ;
                $record = array('now'=>$diff_arr[$key]);
                $label = urlencode($label);
                $diff[$label] = $record;
            }
        }
        return $diff;
    }
}